﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using DevManager.Models;
using DevManager.Services;
using DynamicData;

namespace DevManager.ViewModels.Pages;

public class RunCodePageModel : PageModelBase
{
    private readonly List<InstalledModel> Models;
    private readonly Dictionary<string, string> RunDictionary;
    public override string PageName() => "代码运行";
    public List<string> Platforms { get; set; }
    public ObservableCollection<string> Versions { get; set; }
    private string _platform = "";

    public string Platform
    {
        get => _platform;
        set
        {
            if (string.IsNullOrEmpty(value)) return;
            SetField(ref _platform, value);
            ShellGeneration();
            Versions.Clear();
            Versions.Add(Models.FirstOrDefault(x => x.Name == value)?.Versions!);
        }
    }

    private string _version = "";

    public string Version
    {
        get => _version;
        set
        {
            SetField(ref _version, value);
            ShellGeneration();
        }
    }

    private bool _isFolder;

    public bool IsFolder
    {
        get => _isFolder;
        set => SetField(ref _isFolder, value);
    }

    private string _runPath = "";

    public string RunPath
    {
        get => _runPath;
        set
        {
            SetField(ref _runPath, value);
            ShellGeneration();
        }
    }

    private string _runOrder = "";

    public string RunOrder
    {
        get => _runOrder;
        set => SetField(ref _runOrder, value);
    }

    private string _runResult = "";
    public string RunResult
    {
        get => _runResult;
        set => SetField(ref _runResult,value);
    }
    
    public RunCodePageModel()
    {
        Models = new List<InstalledModel>();
        Platforms = new List<string>();
        Versions = new ObservableCollection<string>();
        RunDictionary = new Dictionary<string, string>();
    }

    public RunCodePageModel(LanguageModel platforms)
    {
        Models = platforms.Installed.Where(x => x.IsInstalled).ToList();
        RunDictionary = platforms.Info.RunOrders;
        Platforms = Models.Select(x => x.Name).ToList();
        Versions = new ObservableCollection<string>();
    }

    private void ShellGeneration()
    {
        if (string.IsNullOrEmpty(Version) || string.IsNullOrEmpty(Platform) || string.IsNullOrEmpty(RunPath))
            return;
        var o = RunDictionary[Platform];
        if(string.IsNullOrEmpty(o))return;
        var l = "";
        if(string.IsNullOrEmpty(o))return;
        RunOrder = o.Replace("&f",RunPath).Replace("&l",l);
    }

    public async void Choose()
    {
        if (IsFolder)
        {
            var top = TopLevel.GetTopLevel(new Window());
            if (top == null) return;
            var a = await top.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions()
                { AllowMultiple = false });
            var result = a.FirstOrDefault();
            if (result == null) return;
            RunPath = result.Path.LocalPath;
        }
        else
        {
            var top = TopLevel.GetTopLevel(new Window());
            if (top == null) return;
            var a = await top.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
                { AllowMultiple = false });
            var result = a.FirstOrDefault();
            if (result == null) return;
            RunPath = result.Path.LocalPath;
        }
    }

    public async void RunShell()
    {
        if(string.IsNullOrEmpty(RunOrder))return;
        RunResult = await OrderRunService.Run(RunOrder.Split("\n"),IsFolder);
    }

    public void CopyShell()
    {
        if(string.IsNullOrEmpty(RunOrder))return;
        var top = TopLevel.GetTopLevel(new Window());
        if (top == null) return;
        top.Clipboard?.SetTextAsync(RunOrder);
    }
}